package com.gjy.nwpufindseats.Fragment;

import com.gjy.nwpufindseats.util.NetInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.gjy.nwpufindseats.Activity_grade;
import com.gjy.nwpufindseats.MainActivity;
import com.gjy.nwpufindseats.R;

public class Fragment_gradelist extends Fragment implements OnClickListener {
	private View view;
	private TextView thisTerm;
	private TextView properties;
	
	private TextView notPass;
	private TextView pass;
	public static SharedPreferences preferences;
	public String xhString;
	public String passwordString;
	public static NetInfo netInfo = new NetInfo();
	Context mainContext;
	 @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    		Bundle savedInstanceState) {
	    	view = inflater.inflate(R.layout.fragment_gradelist, container, false);
	    	mainContext = getActivity();
	    	
	    	thisTerm = (TextView) view.findViewById(R.id.thisTerm);
	    	thisTerm.setOnClickListener(this); 
	    	properties = (TextView) view.findViewById(R.id.properties);
	    	properties.setOnClickListener(this); 
	    	notPass = (TextView) view.findViewById(R.id.notPass);
	    	notPass.setOnClickListener(this); 
	    	pass = (TextView) view.findViewById(R.id.pass);
	    	pass.setOnClickListener(this);
	    	
	    	return view;
	    }
	     
		@Override
		public void onClick(View v) {
			preferences=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
	    	xhString = preferences.getString("xh", "0");//未登录，则返回0，登录返回学号
			if(!netInfo.isNetConnect(mainContext))
	    	{
	    		Fragment newContent = new Fragment_netFailure();
	    		switchFragment(newContent);
	    	}
			else if("0".equals(xhString))
		    	{
		    		Fragment newContent = new Fragment_login();
		    		switchFragment(newContent);	    		
		    		Toast.makeText(getActivity(), "您还没有登录，请先登录", Toast.LENGTH_SHORT).show();
		    	}
			else{	    	
					Intent intent = null;
					Bundle bundle = new Bundle();	
					switch (v.getId()) {
					case R.id.thisTerm:
						bundle.putInt("gradeType",  0);//0代表本学期成绩				
						break;
					case R.id.properties:
						bundle.putInt("gradeType",  1);//1代表属性成绩
						break;
					case R.id.notPass:
						bundle.putInt("gradeType",  2);//2代表不及格
					break;
					case R.id.pass:
						bundle.putInt("gradeType",  3);//3代表所有及格成绩			
						break;	
					default:
					break;
					}
					intent = new Intent(getActivity(),Activity_grade.class);
					intent.putExtras(bundle);
					startActivity(intent);
			}
			
		}
	     private void switchFragment(Fragment fragment) {
	  		if (getActivity() == null)
	  			return;	
	  			MainActivity ra = (MainActivity) getActivity();
	  			ra.switchContent(fragment);
	  		
	  	}

}
